package com.zhixuan.autoui.sample;

import android.content.Context;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.Display;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import cd.zhixuan.utils.recycler.RecyclerGridSelectTextView;
import cd.zhixuan.utils.recycler.adapter.BaseRecyclerAdapter;
import cd.zhixuan.utils.recycler.adapter.RecyclerListAdapter;
import cd.zhixuan.utils.recycler.adapter.listener.OnBindHolder;
import cd.zhixuan.utils.recycler.adapter.listener.OnItemClickListener;
import com.tinker.coresdk.manager.ZXSystemManager;
import com.tinker.coresdk.release.iface.SystemObserver;
import com.zhixuan.autoui.R;

/**
 * 亮度调节
 * author: HuZC
 * Date: 2021/11/23
 * Description:
 */
public class BrightnessLevelActivity extends BaseTestActivity {
    private static final String TAG = "BrightnessLevelActivity";
    private SeekBar seekBar;
    private RecyclerGridSelectTextView<String> rgstView;

    @Override
    protected int onContentLayout() {
        return R.layout.sample_activity_brightness_level;
    }

    @Override
    protected void onCreated(Bundle savedInstanceState) {

        seekBar = findViewById(R.id.sbar);
        rgstView = findViewById(R.id.rgst_view);

        RecyclerListAdapter<String> adapter = rgstView.getAdapter();
        adapter.setChoiceState(BaseRecyclerAdapter.ChoiceState.NONE);
        adapter.setChoiceMode(BaseRecyclerAdapter.ChoiceMode.NONE);
        adapter.bindHolder(new OnBindHolder<RecyclerView.ViewHolder, String>() {
            @Override
            public void onBind(RecyclerView.ViewHolder viewHolder, String s, int i) {
                TextView text = viewHolder.itemView.findViewById(R.id.text);
                text.setText(s);
            }
        });
        adapter.setOnItemClickListener(new OnItemClickListener<RecyclerView.ViewHolder, String>() {
            @Override
            public boolean onItemClick(RecyclerView.ViewHolder viewHolder, String s, int i) {
                switch (s) {
                    case "获取屏幕亮度":
                        int curGamma = getCurLight();
                        addMessage("屏幕亮度：" + curGamma);
                        break;
                    case "获取屏幕亮度范围":
                        int minGamma = getMinLight();
                        int maxGamma = getMaxLight();
                        addMessage("屏幕亮度范围：" + minGamma + " ~ " + maxGamma);
                        break;
                    case "增加屏幕亮度":
                        increaseLight(true);
                        break;
                    case "降低屏幕亮度":
                        increaseLight(false);
                        break;
                }
                return true;
            }
        });
        adapter.setDataList(new String[]{"获取屏幕亮度", "获取屏幕亮度范围", "增加屏幕亮度", "降低屏幕亮度"});
        //

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                try {
                    ZXSystemManager.getInstance().requestSetBacklight(progress);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //
        ZXSystemManager.getInstance().setTool(new SystemObserver() {
            @Override
            public void onSetBacklight(int level) {
                addMessage("收到设置背光亮度通知： level=" + level);
            }
        });

        int minGamma = getMinLight();
        int maxGamma = getMaxLight();
        int curGamma = getCurLight();
        seekBar.setMin(minGamma);
        seekBar.setMax(maxGamma);
        seekBar.setProgress(curGamma);

    }

    private int getMinLight() {
        try {
            return ZXSystemManager.getInstance().requestGetMinBacklight();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return -1;
    }

    private int getMaxLight() {
        try {
            return ZXSystemManager.getInstance().requestGetMaxBacklight();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return -1;
    }

    private int getCurLight() {
        printDisplayId();
        try {
            return ZXSystemManager.getInstance().requestGetBacklight();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return -1;
    }

    private void increaseLight(boolean increase) {
        try {
            ZXSystemManager.getInstance().requestStepBacklight(increase);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void printDisplayId() {


        printDisplayId("current", this);
        String pkgName = "com.zhixuan.android.zxcarsetting";
        try {
            Context packageContext = createPackageContext(pkgName, CONTEXT_IGNORE_SECURITY);
            printDisplayId("pkg", packageContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void printDisplayId(String text, Context context) {
        int displayId = -1;
        try {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display defaultDisplay = wm.getDefaultDisplay();
            displayId = defaultDisplay.getDisplayId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        addMessage(text + " displayId=" + displayId);
    }
}
